package com.sanhuo.idms.controller;

import com.sanhuo.file.service.FileService;
import com.sanhuo.idms.bo.PersonalUpdateBO;
import com.sanhuo.idms.entity.*;
import com.sanhuo.idms.service.RepairPersonalService;
import com.sanhuo.idms.vo.RepairPageVO;
import com.sanhuo.idms.vo.RepairPersonalVO;
import com.sanhuo.idms.vo.TaskNumVO;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.lib.exception.GlobalException;
import com.sanhuo.security.entity.User;
import com.sanhuo.security.service.UserService;
import com.sanhuo.security.uitls.UserHolder;
import com.sanhuo.triild.page.Page;
import com.sanhuo.triild.page.PageSearch;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.office.OfficeUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("repair")
@RestController
public class RepairPersonalController {
    @Autowired
    private RepairPersonalService repairPersonalService;
    @Autowired
    private FileService fileService;
    @Autowired
    private UserService userService;

    @Transactional
    @RequestMapping("excal/save")
    public void saveFromExcal(MultipartFile file) throws IOException {
        ExceptionUtil.throwException(BeanUtil.isNull(file),"上传文件不能为空");
        Workbook workbook = OfficeUtil.readFile(file);
        Sheet sheet = workbook.getSheetAt(0);
        Cell isThis = sheet.getRow(0).getCell(0);
        if (!"新增维修人员模板表".equals(isThis.getStringCellValue())) {
            throw new GlobalException("请上传正确的文件");
        }
        Row title = sheet.getRow(2);
        for (int rowNum = 3; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            if (row != null) {
                int cellNum = row.getLastCellNum();
                for (int num = 0; num < cellNum; num++) {
                    Cell cell = row.getCell(num);
                    if (cell != null) {
                        cell.setCellType(CellType.STRING);
                    }
                }
                String id = row.getCell(0).getStringCellValue();
                String name = row.getCell(1).getStringCellValue();
                String gender = row.getCell(2).getStringCellValue();
                String phone = row.getCell(3).getStringCellValue();
                RepairPersonal repairPersonal = new RepairPersonal();
                repairPersonal.setId(id);
                repairPersonal.setGender(gender);
                repairPersonal.setName(name);
                User user = new User();
                user.setId(id);
                user.setName(name);
                user.setPassword("12345678");
                user.setPhone(phone);
                repairPersonal.creatorAndUpdatorInfo(UserHolder.user());
                user.creatorAndUpdatorInfo(UserHolder.user());
                user.setRoleId("04");
                userService.save(user);
                repairPersonalService.save(repairPersonal);

            }
        }
    }

    @RequestMapping("excal")
    public void getExcal(HttpServletRequest req, HttpServletResponse res) throws ParseException, IOException {
        String path = "C:\\repair.xlsx";//路径
        XSSFWorkbook workbook = new XSSFWorkbook();
        //新建工作表
        XSSFSheet sheet = workbook.createSheet("新增维修人员模板");
        //创建行,0表示第一行
        XSSFCellStyle titleStyle = workbook.createCellStyle();
        // 水平居中
        titleStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
        //垂直居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //第一行
        XSSFRow row0 = sheet.createRow(0);
        //创建单元格行号由row确定,列号作为参数传递给createCell;第一列从0开始计算
        //第一个
        XSSFCell cell0 = row0.createCell(0);
        //给单元格赋值
        cell0.setCellValue("新增维修人员模板表");
        cell0.setCellStyle(titleStyle);//设置样式
        XSSFRow row = sheet.createRow(2);
        List<String> title = new ArrayList<>();
        title.add("工号");
        title.add("姓名");
        title.add("性别");
        title.add("手机");
        CellRangeAddress cra = new CellRangeAddress(0, 1, 0, 10);
        sheet.addMergedRegion(cra);
        XSSFCellStyle cellStyle = workbook.createCellStyle();
//        cellStyle.setShrinkToFit(true);
        for (int i = 0; i < title.size(); i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(title.get(i));
            cell.setCellStyle(cellStyle);
            sheet.setColumnWidth(i, 18 * 256);
        }
        //创建输出流
        FileOutputStream fos = new FileOutputStream(new File(path));
        workbook.write(fos);
        workbook.close();
        fos.close();
        fileService.downloadLocalFile(path, "新增维修人员模板.xlsx", req, res);
    }

    @RequestMapping("list")
    public Page<RepairPersonalVO> list(PageSearch pageSearch) {
        return repairPersonalService.list(pageSearch);
    }

    @RequestMapping("update")
    public void update(PersonalUpdateBO bo) throws IllegalAccessException {
        repairPersonalService.update(UserHolder.user(), bo);
    }

    @RequestMapping("detail")
    public RepairPersonalVO detail(String id) {
        return repairPersonalService.
                detailVO(id);
    }

    @RequestMapping("")
    public List<RepairPersonal> all() {
        return repairPersonalService.findAll();
    }

    @RequestMapping("myTask")
    public Page<RepairPageVO> myTask(PageSearch pageSearch) {
        return repairPersonalService.myTask(UserHolder.user(), pageSearch);
    }

    @RequestMapping("taskNum")
    public TaskNumVO taskNum() {
        return repairPersonalService.taskNum(UserHolder.user().getId());
    }
}
